function formmaterProductName(value, row, index) {
    if (row.product) {
        return row.product.productName;
    }
}

function formmaterCosePrice(value, row, index) {
    if (row.product) {
        return row.product.cosePrice;
    }
}

function formmaterSn(value, row, index) {
    if (row.outcomeBill) {
        return row.outcomeBill.sn;
    }
}

function formmaterTotalNumber(value, row, index) {
    if (row.outcomeBill) {
        return row.outcomeBill.totalNumber;
    }
}

function formmaterTotalAmount(value, row, index) {
    if (row.outcomeBill) {
        return row.outcomeBill.totalAmount;
    }
}

function formmaterInputTime(value, row, index) {
    if (row.outcomeBill) {
        return row.outcomeBill.inputTime;
    }
}

function formmaterShopName(value, row, index) {

    if (row.product.shop) {
        return row.product.shop.name;
    }
}

function formatStatus(value, row, index) {

    if (value) {

        if (value == 1) return '待支付';

        if (value == 2) return '待发货';

        return '退款';
    }
}

function openSubDialog() {
    $('#subDialog').dialog("open");
}

function formatOperation(value, row, index) {
    return '<button onclick="openSubDialog()">查看详情</button>';
}

$(function () {

    //表格
    var order_datagrid = $("#order_datagrid");

    //数据表格渲染
    order_datagrid.datagrid({
        url: "/order/list",
        fit: true,
        fitColumns: true,
        border: false,
        rownumbers: true,
        pagination: true,
        singleSelect: true,
        toolbar: '#order_datagrid_toolbar',
        columns: [[
            {field: 'productName', title: '商品名称', width: 80, formatter: formmaterProductName},
            {field: 'sn', title: '单号', width: 120, formatter: formmaterSn},
            {field: 'cosePrice', title: '商品单价', width: 60, formatter: formmaterCosePrice},
            {field: 'totalNumber', title: '数量', width: 60, formatter: formmaterTotalNumber},
            {field: 'totalAmount', title: '销售金额', width: 60, formatter: formmaterTotalAmount},
            {field: 'shop', title: '所属店铺', width: 80, formatter: formmaterShopName},
            {field: 'inputTime', title: '消费时间', width: 160, formatter: formmaterInputTime},
            {field: 'remark', title: '备注', width: 150},
            {field: 'status', title: '状态', width: 60, formatter: formatStatus},
            {field: 'operation', title: '操作', width: 80, formatter: formatOperation}
        ]]
    });




    //1.把方法绑定到一个对象上
    var cmdObj = {
        //订单表格导出
        exportOrderInfos: function () {
            window.location.href = "/order/export";
        },
        //高级查询
        searchOrder: function () {
            //1.获取查询input里面的值
            var keyword = $("#keyword").val();

            //2.调用datagrid重新加载数据,并且带上查询条件  keyword
            order_datagrid.datagrid("load", {keyword: keyword});
        },
        //订单列表刷新
        reloadOrder: function () {
            order_datagrid.datagrid("reload");
        }
    };

    //2.按钮的点击事件通过js来绑定
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        //cmd = "addEmp";
        //cmdObj[cmd]  :根据cmd的值,找到 cmdObj 对应的方法
        //cmdObj[cmd]() : () ,触发方法的执行
        console.log("..cmd:" + cmd);
        cmdObj[cmd]();
    });

});

